package com.xuecheng.content.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.xuecheng.content.config.SmsTemplate;
import com.xuecheng.content.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    // 发送短信
    @Override
    public void sendSms(String phone) {
        // 1.生成六位随机数
        String code = RandomUtil.randomNumbers(6);
        // 2.调用阿里云sms发短信
        code = "123456";
        // smsTemplate.sendSms(phone, code);
        // 3.将验证码存入redis
        redisTemplate.opsForValue().set("code:" + phone, code, Duration.ofSeconds(90));
    }
}
